<?
/**
* Create the minimalistic connection
* And data tranfer to MySQL
*
* @author Adesanto Asman
* @version 1.0
* @license Free
* @copyright Copyright (c)2007, Adesanto Asman
* @package MySQL
*
*/

include_once "DbVars.php";

/**
* Extends DbVars
* @package MySQL
* @see DbVars
*
*/
class MySQL extends DbVars{

  /**
  * Init all necessary variables
  *
  * @param string $userName
  * @param string $userPwd
  * @param string $userSid
  * @param string $userComment
  *
  */
  function init($userName,
                $userPwd,
                $userSid,
                $userComment){

    $this->userName     = $userName;
    $this->userPwd      = $userPwd;
    $this->userSid      = $userSid;
    $this->userComment  = $userComment;
  }

  /**
  * It returns false if fail
  * @param void
  * @return boolean
  *
  */
  function connect(){

    if($this->dbConn = mysql_connect($this->userSid,
                                     $this->userName,
                                     $this->userPwd)){

      $this->selectDb($this->userName);

      return true;
    }else{

      Misc::usrDie("Error: Connection failed, ". mysql_error());
    }
  }

  /**
  * Select DB
  *
  * Although there is native function
  * to select a DB but i create one
  *
  * @param  string  $dbName
  *
  */
  function selectDb($dbName){

    if(mysql_select_db($dbName, $this->dbConn)){

      return true;
    }else{

      Misc::usrDie("Error: Can't use ".$dbName.", ". mysql_error());
    }

  }

  /**
  * Disconnect the DB
  * @param void
  *
  */
  function dc(){

    mysql_close($this->dbConn);
  }

  /**
  * Execute the query and the results
  * will be passed by reference to &$nrows and &$result
  *
  * @param string   $query
  * @param integer  $nrows
  * @param array    $result
  * @param boolean  $isDbg
  *
  */
  function execFetchQuery(&$query,
                          &$nrows,
                          &$result,
                           $isDbg = _IS_DEBUG){

    $bufOrgKey    = array(); /* Original name of field      */
    $bufUpperKey  = array(); /* Original key being uppered  */
    $bufResult    = array();

    if($isDbg){

      $dbgInfo = debug_backtrace();
      echo	"<br/>(".$dbgInfo[0]["file"].":".$dbgInfo[0]["line"].") ".$query." <br/>\n";
    }

    $res = mysql_query($query);

    if($res){

      $nrows  = mysql_num_rows($res);

      /**
      * Find out how many element
      *
      */
      $nElement = mysql_num_fields($res);

      /**
      * change array' key case to upper and
      * extract the array' key
      *
      */
      for($iElm = 0; $iElm < $nElement; $iElm++){

        $bufOrgKey[$iElm]   = mysql_field_name($res, $iElm);
        $bufUpperKey[$iElm] = strtoupper($bufOrgKey[$iElm]);
      }

      for($iRow = 0; $iRow < $nrows; $iRow++){

        $tmpResult = mysql_fetch_assoc($res);

        for($iElm = 0; $iElm < $nElement; $iElm++){

          $oKey = $bufOrgKey[$iElm];   /* Original key     */
          $uKey = $bufUpperKey[$iElm]; /* uppered case key */

          $result[$uKey][$iRow] = $tmpResult[$oKey];
        }
      }

      mysql_free_result($res);
    }else{

      $msg  = "Error: Invalid query: ".
              mysql_error().
              "\n";

      if($isDbg){

        $msg .= "Whole query: ".$query;
      }

      Misc::usrDie($msg);
    }
  }

  /**
  * Exec query not a fetch type
  * it accomodates UPDATE, INSERT, DELETE, CREATE, MODIFY, etc
  * Which not returns any result of rows of data.
  *
  * @param string   $query
  * @param boolean  $isDbg true show the query,
                           false dont show.
  * @return boolean false when it fails
  *
  */
  function execQuery( $query,
                      $isDbg = _IS_DEBUG){

    if($isDbg){

      $dbgInfo = debug_backtrace();
      echo	"<br/>(".$dbgInfo[0]["file"].":".$dbgInfo[0]["line"].") ".$query." <br/>\n";
    }

    MySQL::startTransaction();

    $res = mysql_query($query);

    if($res){

      /**
      * if there is no error meaning
      * the query is success
      *
      */
    }else{

      $msg  = "Error: Invalid query: ".
              mysql_error().
              "\n";

      if($isDbg){

        $msg .= "Whole query: ".$query;
      }

      Misc::usrDie($msg);
    }
  }

  /**
  *
  * @param void
  * @return array
  *
  */
  function result(){

    return $this->result;
  }

  /**
  *
  * @param void
  * @return integer
  *
  */
  function nrows(){

    return $this->nrows;
  }

  /**
  *
  * @param void
  * @return string
  *
  */
  function getStrConn(){

    return $this->dbConn;
  }

  /**
  * Set the autocommit off
  *
  * @param  void
  *
  */
  function startTransaction(){

    $res = mysql_query("SET AUTOCOMMIT=0");

    if($res){

      /* Start the transaction */
    }else{

      Misc::usrDie("Error: Autocommit->0 failed");
    }
  }

  /**
  *
  * @param void
  *
  */
  function commit(){

    /**
    * default query to commit
    *
    */
    $res = mysql_query("COMMIT");

    if($res){

      /* Do nothing */
    }else{

      Misc::usrDie("Error: Commit failed");
    }
  }

  /**
  * get the status
  *
  * @param void
  * @return boolean
  *
  */
  function getStatus(){

    return $this->status;
  }

  /**
   * set Status manually
   *
   * @param boolean $status
   */
  function setStatus($status){

    $this->status = $status;
  }
}
?>